<!DOCTYPE html>
<html>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" type="text/css" href="css/style.css">
        <head>
                <title>An African Language Short Story Competition</title>
        </head>
<body>
<?php include 'submitdatabaseinfo1.php';?>
<?php include 'getlanguage.php';?>

<?php

if (isset($_POST['getevaluations'])) {
if (isset($_POST['pass'])){
	$pass = $_POST['pass'];
}else{
	$pass="";
}
if($pass="ChangeMeToSomethingSecure"){
// Create connections
$conn = mysqli_connect($servername, $username, $password, $dbvotes);
mysqli_set_charset($conn, "utf8");
// Check connection
if (!$conn) {
  die("Database connection failed: " . mysqli_connect_error());
}

$conn2 = mysqli_connect($servername, $username, $password, $dbname);
mysqli_set_charset($conn2, "utf8");
// Check connection
if (!$conn) {
  die("Database connection failed: " . mysqli_connect_error());
}

$sql = "SELECT storyid, comment, grammarandspelling, themeandplot, style, culturalrelevance, overallimpression FROM votes GROUP BY voterid, storyid";
$result = mysqli_query($conn,$sql);
$rows = mysqli_num_rows($result);
echo "There are " . $rows . " evaluations. <br>";
if ( $rows > 0 ) {
	echo "<table>";
	echo "<tr><th>Story Title</th>
		  <th>Translation</th>
		  <th>Language</th>
		  <th>Grammar and spelling</th>
		  <th>Theme and plot</th>
		  <th>Style</th>
                  <th>Cultural relevance</th>
		  <th>Overall Impression</th>
                  <th>Total</th>
                  <th>Comment</th></tr>";
// Create empty arrays to calculate averages
$storyids = array();
$storyevaluationsum = array();
$storyevaluationcount = array();
$storytitle = array();
$storylanguage = array();
$storytransltionn = array();

while($row = mysqli_fetch_array($result)){
	                $sql2 = "SELECT title,language,translation FROM originalstories WHERE primarykey =" . $row["storyid"];
			$result2 = mysqli_query($conn2,$sql2);
			$row2 = mysqli_fetch_array($result2);
	                $totalscore= $row["grammarandspelling"] +
				     $row["themeandplot"] +
				     $row["style"] +
				     $row["culturalrelevance"] +
				     $row["overallimpression"];
			// store score
			$arrlength = count($storyids);
			$found = 0;
			$arrindex = $arrlength;
			for($i = 0; $i < $arrlength; $i++) {
				if ( $storyids[$i] === $row["storyid"] ) {
					$found = 1;
					$arrindex = $i;
				}
			}
			if($found==1){
				$storyevaluationsum[$arrindex] += $totalscore;
				$storyevaluationcount[$arrindex] += 1;
			}else{
				$storyids[$arrindex] = $row["storyid"];
				$storyevaluationsum[$arrindex] = $totalscore;
				$storyevaluationcount[$arrindex] = 1;
				$storytitle[$arrindex] = $row2["title"];
				$storytranslation[$arrindex] = $row2["translation"];
				$storylanguage[$arrindex] = getlanguage($row2["language"]);
			}
			// Display table on webpage with scores and comments
			$language = getlanguage($row2["language"]);
			echo "<tr><td> " . $row2["title"] . "</td>
				  <td> " . $row2["translation"] . "</td>
				  <td> " . $language . "</td>
				  <td> " . $row["grammarandspelling"] . "</td>
                                  <td> " . $row["themeandplot"] . "</td>
                                  <td> " . $row["style"] . "</td>
                                  <td> " . $row["culturalrelevance"] . "</td>
				  <td> " . $row["overallimpression"] . "</td>
                                  <td> " . $totalscore . "</td>
                                  <td> " . $row["comment"] ."</td>
                              </tr>";
	}
echo "</table>";
// Table with average scores
echo "<table>";
echo "<tr><th>Story Title</th>
	<th>Language</th>
        <th>Translation</th>
        <th>Number of evaluations</th>
        <th>Average score</th>
      </tr>";
	for($i = 0; $i <= $arrlength; $i++) {
		$average = $storyevaluationsum[$i]/$storyevaluationcount[$i];
		echo "<tr><td> " . $storytitle[$i] . "</td>
			  <td> " . $storylanguage[$i] . "</td>
			  <td> " . $storytranslation[$i] . "</td>
			  <td> " . $storyevaluationcount[$i] . "</td>
			  <td> " . $average . "</td></tr>";
           }
echo "</table>";

}
mysqli_close($conn);
exit;
}
}
?>
<header>
                        <h1>Tuvute Pamoja
                        <a href="https://deeplearningindaba.com">
                         <img src="images/deeplearningindaba.svg" alt="Deeplearning Indaba" style="height:60px">
			</a></h1>
</header>
<form method="post">
Password<input type="password" name="pass"></input><br/>
<input type="submit" name="getevaluations" value="Get Evaluations"/>
</form>
</body>
</html>
